package com.kawaiior.asoulworld.events;

import com.kawaiior.asoulworld.IdlFramework;
import com.kawaiior.asoulworld.capability.*;
import com.kawaiior.asoulworld.item.ModItems;
import com.kawaiior.asoulworld.items.weapon.ItemMiyuWand;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Mod.EventBusSubscriber(modid = IdlFramework.MODID)
public class CapabilityEvent {

    @SubscribeEvent
    public static void onAttachItemCapabilities(AttachCapabilitiesEvent<ItemStack> event){
        if (event.getObject().getItem() instanceof ItemMiyuWand){
            MiyuPowerProvider provider = new MiyuPowerProvider();
            event.addCapability(new ResourceLocation(IdlFramework.MODID + "miyu_power"), provider);
        }
    }

    @SubscribeEvent
    public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event){
        if (event.getObject() instanceof EntityPlayer){
            AvaPowerProvider provider = new AvaPowerProvider();
            event.addCapability(new ResourceLocation(IdlFramework.MODID + "ava_power"), provider);

            AsoulAirPowerProvider asoulAirPowerProvider = new AsoulAirPowerProvider();
            event.addCapability(new ResourceLocation(IdlFramework.MODID + "asoul_air_power"), asoulAirPowerProvider);

            AsoulTimePowerProvider asoulTimePowerProvider = new AsoulTimePowerProvider();
            event.addCapability(new ResourceLocation(IdlFramework.MODID + "asoul_time_power"), asoulTimePowerProvider);
        }
    }

    @SubscribeEvent
    public static void onPlayerClone(PlayerEvent.Clone event){
        AvaPower instance = event.getEntityPlayer().getCapability(CapabilityRegistryHandler.AVA_POWER, null);
        AvaPower original = event.getOriginal().getCapability(CapabilityRegistryHandler.AVA_POWER, null);
        if (instance != null && original != null){
            instance.setPower(original.getPower());
        }

        AsoulAirPower asoulAirPowerInstance = event.getEntityPlayer().getCapability(CapabilityRegistryHandler.ASOUL_AIR_POWER, null);
        AsoulAirPower asoulAirPowerOriginal = event.getOriginal().getCapability(CapabilityRegistryHandler.ASOUL_AIR_POWER, null);
        if (asoulAirPowerInstance != null && asoulAirPowerOriginal != null) {
            asoulAirPowerInstance.setHealth(asoulAirPowerOriginal.getHealth());
            asoulAirPowerInstance.setXYZ(asoulAirPowerOriginal.getPosX(), asoulAirPowerOriginal.getPosY(), asoulAirPowerOriginal.getPosZ());
        }

        AsoulTimePower asoulTimePowerInstance = event.getEntityPlayer().getCapability(CapabilityRegistryHandler.ASOUL_TIME_POWER, null);
        AsoulTimePower asoulTimePowerOriginal = event.getOriginal().getCapability(CapabilityRegistryHandler.ASOUL_TIME_POWER, null);
        if (asoulTimePowerInstance != null && asoulTimePowerOriginal != null) {
            asoulTimePowerInstance.setAllTime(asoulTimePowerOriginal.getTime());
        }
    }

    @SubscribeEvent
    public static void onPlayerJoin(EntityJoinWorldEvent event){
        Entity entity = event.getEntity();
        if (!entity.world.isRemote && entity instanceof EntityPlayer){

            AsoulTimePower asoulTimePower = entity.getCapability(CapabilityRegistryHandler.ASOUL_TIME_POWER, null);
            if (asoulTimePower != null){
                ((EntityPlayer)entity).getCooldownTracker().setCooldown(ModItems.ITEM_AS_TIME_SWORD, asoulTimePower.getCool()*20);
            }

//            AvaPower power = entity.getCapability(CapabilityRegistryHandler.AVA_POWER, null);
//            String message = "You AVA power is" + power.getPower();
//
//            TextComponentString text = new TextComponentString(message);
//            entity.sendMessage(text);
        }
    }

}
